﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;

namespace caculate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class PublicValue
        {
            public static string prtaccount;
            public static string prtpassword;
            public static int log_in;
            public static string chdaccount;
            public static string chdpassword;
            public static string chdclass;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            this.MinimizeBox = true;

        }


        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NewUser frm_newuser = new NewUser();
            frm_newuser.Owner = this;
            frm_newuser.ShowDialog();
        }


        private void parent_account_TextChanged(object sender, EventArgs e)
        {
            PublicValue.prtaccount = parent_account.Text;
        }

        private void parent_password_TextChanged(object sender, EventArgs e)
        {
            PublicValue.prtpassword = parent_password.Text;
        }

        private void login_Click(object sender, EventArgs e)
        {
            string conString = "DSN=test;UID=root;PWD=123456"; //ODBC的数据源名
            //建立连接
            OdbcConnection con1 = new OdbcConnection(conString);
            OdbcConnection con2 = new OdbcConnection(conString);
            con1.Open();
            con2.Open();
            //查询数据库
            string judge = "select * from TeacherMessage where TeacherAccount='" + PublicValue.prtaccount + "'and TeacherPassword='" + PublicValue.prtpassword + "'";
            OdbcCommand com1 = new OdbcCommand(judge, con1);
            OdbcDataReader sdr = com1.ExecuteReader();//搜索结果

            if (sdr.Read())
            {
                PublicValue.log_in = 1;
                string parent_name = "select TeacherName from TeacherMessage where TeacherAccount='" + PublicValue.prtaccount + "'";
                OdbcCommand com2 = new OdbcCommand(parent_name, con2);

                String name = Convert.ToString(com2.ExecuteScalar()); //查找使用者姓名             
                /*parent_account.Text = "";
                parent_password.Text = "";//清空textbox*/
                MessageBox.Show(String.Format("亲爱的{0}，欢迎使用xx速算软件: ", name));
                //this.Hide();

                //打开操作界面
                parent_operate frm_prtoperate = new parent_operate();
                frm_prtoperate.Owner = this;
                frm_prtoperate.ShowDialog();
            }
            else
            {
                MessageBox.Show("用户名或密码错误，请重试");
            }
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NewUser frm_newuser = new NewUser();
            frm_newuser.Owner = this;
            frm_newuser.ShowDialog();
        }

        private void Login_Focus(object sender, EventArgs e)
        {
            parent_account.Focus(); // 获取输入账号焦点
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string conString = "DSN=test;UID=root;PWD=123456"; //ODBC的数据源名
            //建立连接
            OdbcConnection con1 = new OdbcConnection(conString);
            OdbcConnection con2 = new OdbcConnection(conString);
            OdbcConnection con3 = new OdbcConnection(conString);
            con1.Open();
            con2.Open();
            con3.Open();
            //查询数据库
            string judge = "select * from childrenMessage where childrenAccount='" + PublicValue.chdaccount + "'and childrenPassword='" + PublicValue.chdpassword + "'";
            OdbcCommand com1 = new OdbcCommand(judge, con1);
            OdbcDataReader sdr = com1.ExecuteReader();//搜索结果

            if (sdr.Read())
            {
                string sql2 = "select classID from childrenMessage where childrenAccount='" + PublicValue.chdaccount + "'";

                OdbcCommand com3 = new OdbcCommand(sql2, con3);
                PublicValue.chdclass = Convert.ToString(com3.ExecuteScalar());
                string sql = "select childrenName from childrenMessage where childrenAccount='" + PublicValue.chdaccount + "'";
                OdbcCommand com2 = new OdbcCommand(sql, con2);

                String name = Convert.ToString(com2.ExecuteScalar()); //查找使用者姓名
                                                                      //
                /*children_account.Text = "";
                children_password.Text = "";//清空textbox*/
                MessageBox.Show(String.Format("亲爱的{0}，欢迎使用xx速算软件: ", name));
                //this.Hide();
                //打开操作界面
                children_operate frm_chdoperate = new children_operate();
                frm_chdoperate.Owner = this;
                frm_chdoperate.ShowDialog();
            }
            else
            {
                MessageBox.Show("用户名或密码错误，请重试");
            }
        }

        private void children_account_TextChanged(object sender, EventArgs e)
        {
            PublicValue.chdaccount = children_account.Text;
        }

        private void children_password_TextChanged(object sender, EventArgs e)
        {
            PublicValue.chdpassword = children_password.Text;
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NewUser frm_newuser = new NewUser();
            frm_newuser.Owner = this;
            frm_newuser.ShowDialog();
        }

        private void passwordapr_CheckedChanged(object sender, EventArgs e)
        {
            if (passwordapr.Checked)
            {
                parent_password.PasswordChar = new char();
            }
            else
            {
                parent_password.PasswordChar = '●';
            }
        }

        private void student_passwordapr_CheckedChanged(object sender, EventArgs e)
        {
            if (student_passwordapr.Checked)
            {
                children_password.PasswordChar = new char();
            }
            else
            {
                children_password.PasswordChar = '●';
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {
           
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void parent_account_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==(char)13)
            {
                this.login_Click(login,null);
            }
        }

        private void parent_password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.login_Click(login, null);
            }
        }

        private void children_account_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button2_Click(button2, null);
            }
        }

        private void children_password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button2_Click(button2, null);
            }
        }

        private void student_passwordapr_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button2_Click(button2, null);
            }
        }

        private void passwordapr_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.login_Click(login, null);
            }
        }
    }
}
